const mongoConnected = require("../lib/mongoose_connect");
const mongoModels = require("../lib/mongoose_schemas");
const fs = require("fs-extra");
const path = require("path");
const lodash = require("lodash");

mongoConnected.then(async $ => {
  let matches = await mongoModels.Match.find({
    map_name: "Savage_Main",
    team_size: 4
  });
  debugger
  let players = {
  };
  for (let mi of matches.map(e => e.match_id)) {
    let o_matchend = await mongoModels.Event.findOne({
      match_id: mi,
      _T: "LogMatchEnd"
    });
    // debugger
    for (let x of o_matchend._doc.characters.map(e => e.character)) {
      // debugger
      let account_id = x.accountId;
      if (account_id.startsWith("account")) {
        if (!players[account_id]) {
          players[account_id] = {
            name: x.name,
            account_id: account_id,
            match_count: 1,
            ranking_count: x.ranking,
            qianwu: x.ranking <= 5 ? 1 : 0
          }
        } else {
          players[account_id].match_count += 1;
          players[account_id].ranking_count += x.ranking
          if (x.ranking <= 5) {
            players[account_id].qianwu += 1
          }
        }
      }
    }
  }
  let they = Object.values(players);
  they.forEach(e => {
    e.ave_ranking = e.ranking_count / e.match_count;
    e.ave_qianwu = e.qianwu/e.match_count;
  });
  debugger
  fs.writeFile(path.join(__dirname, "../test", "0815-每个玩家的参与数和平均排名.json"), JSON.stringify(they), () => {
    debugger
  })

})

